package com.tian.recycler.fragment

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tian.recycler.R
import com.tian.recycler.databinding.FragmentItemShowAnimBinding

/**
 * @author tiansongtao,Email:769001510@qq.com, created on 2021/3/18 17:20
 *
 * recyclerview列表初始化时的动画
 */
class ItemInitAnimFragment : Fragment() {

    private lateinit var b: FragmentItemShowAnimBinding

    private val data: MutableList<String> by lazy {
        val t = mutableListOf<String>()
        for (i in 0..3) {
            t.add("tiansong $i")
        }
        t
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        b = DataBindingUtil.inflate(inflater, R.layout.fragment_item_show_anim, container, false)
        b.rvItemShowAnim.layoutManager = LinearLayoutManager(requireContext())
        b.rvItemShowAnim.adapter = MyAdapter()
//        val default = DefaultItemAnimator().also {
//            it.addDuration = 1000
//            it.removeDuration = 1000
//            it.moveDuration = 1000
//        }
//        b.rvItemShowAnim.itemAnimator = default
//        b.rvItemShowAnim.postDelayed({
//            data.add(0, "add nihao")
//            b.rvItemShowAnim.adapter?.notifyItemInserted(0)
//        }, 5000)
////        b.rvItemShowAnim.postDelayed({
////            data.removeAt(1  )
////            b.rvItemShowAnim.adapter?.notifyItemRemoved(1)
////        }, 8000)
//        b.rvItemShowAnim.postDelayed({
//            val str = data.removeAt(data.size-1)
//            data.add(0, str)
//            b.rvItemShowAnim.adapter?.notifyItemMoved(data.size-1, 0)
//        }, 8000)
        return b.root
    }


    inner class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val textView = TextView(parent.context)
            val lp = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 150).also {
                it.setMargins(30, 30, 30, 30)
            }
            textView.layoutParams = lp
            textView.gravity = Gravity.CENTER
            textView.setBackgroundColor(Color.BLUE)
            textView.textSize = 20f
            textView.setTextColor(Color.WHITE)
            return MyViewHolder(textView)
        }



        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder.itemView as TextView).text = "${data[position]} --- $position"
        }

        override fun getItemCount() = data.size

    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)


}